package com.ls.fw.maven;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public abstract class BaseMvn
{
  private static final String PARENT = "parent";
  private static final String VERSION = "version";
  private static final String GROUP_ID = "groupId";
  private static final String ARTIFACT_ID = "artifactId";
  private static final String PACKAGING = "packaging";
  private final String jarPath;
  public static final int BUFFER_SIZE = 4096;
  
  public BaseMvn(String jafPath)
  {
    this.jarPath = jafPath;
  }
  
  public void exe()
    throws IOException
  {
    File f = new File(this.jarPath);
    if (f.isDirectory())
    {
      File[] strs = f.listFiles();
      File[] arrayOfFile1;
      int j = (arrayOfFile1 = strs).length;
      for (int i = 0; i < j; i++)
      {
        File name = arrayOfFile1[i];
        if (name.isFile()) {
          if (name.getName().endsWith(".pom"))
          {
            System.out.println();
            System.out.println("pomName:" + name.getAbsolutePath());
            exe(getPOMFile(name.getAbsolutePath()));
          }
          else
          {
            installOne(name.getAbsolutePath());
          }
        }
      }
    }
    else
    {
      installOne(this.jarPath);
    }
  }
  
  private POM getPOMFile(String path)
  {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    POM p = new POM();
    p.setJarName(path);
    p.setPomFile(path);
    try
    {
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(path);
      NodeList dogList = doc.getElementsByTagName("project");
      Node dog = dogList.item(0);
      setValue(p, dog, false);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return p;
  }
  
  private void setValue(POM p, Node dog, boolean isParent)
  {
    int index = 4;
    for (Node node = dog.getFirstChild(); node != null; node = node.getNextSibling()) {
      if (node.getNodeType() == 1)
      {
        String name = node.getNodeName();
        if ("parent".equals(name))
        {
          setValue(p, node, true);
        }
        else
        {
          String value = "";
          try
          {
            value = node.getTextContent();
          }
          catch (DOMException e)
          {
            e.printStackTrace();
          }
          if (((!isParent) || (p.getVersion() == null) || (p.getVersion().equals(""))) && 
            ("version".equalsIgnoreCase(name)))
          {
            if (isBlank(value)) {
              p.setVersion(value);
            } else if ((!isBlank(value)) && (!isParent)) {
              p.setVersion(value);
            }
            index--;
          }
          else if (((!isParent) || (p.getGroupId() == null) || (p.getGroupId().equals(""))) && 
            ("groupId".equalsIgnoreCase(name)))
          {
            if (isBlank(value)) {
              p.setGroupId(value);
            } else if ((!isBlank(value)) && (!isParent)) {
              p.setGroupId(value);
            }
            index--;
          }
          else if (((!isParent) || (p.getArtifactId() == null) || (p.getArtifactId().equals(""))) && 
            ("artifactId".equalsIgnoreCase(name)))
          {
            if (isBlank(value)) {
              p.setArtifactId(value);
            } else if ((!isBlank(value)) && (!isParent)) {
              p.setArtifactId(value);
            }
            index--;
          }
          else if (((!isParent) || (p.getPackaging() == null) || (p.getPackaging().equals(""))) && 
            ("packaging".equalsIgnoreCase(name)))
          {
            if (isBlank(value)) {
              p.setPackaging(value);
            } else if ((!isBlank(value)) && (!isParent)) {
              p.setPackaging(value);
            }
            index--;
          }
          if (index <= 0) {
            break;
          }
        }
      }
    }
  }
  
  private void installOne(String jarName)
    throws IOException
  {
    System.out.println();
    System.out.println("jarName:" + jarName);
    exe(getPOM(jarName));
  }
  
  protected boolean isBlank(String str)
  {
    return (str != null) && (!"".equals(str));
  }
  
  public abstract void exe(POM paramPOM);
  
  private POM getPOM(String jarName)
    throws IOException
  {
    JarFile jarFile = new JarFile(jarName);
    Enumeration enum1 = jarFile.entries();
    JarEntry entry = null;
    JarEntry pomEntry = null;
    int need = 2;
    while (enum1.hasMoreElements())
    {
      JarEntry entry1 = (JarEntry)enum1.nextElement();
      if (entry1.getName().endsWith("pom.properties"))
      {
        entry = entry1;
        need--;
      }
      if (entry1.getName().endsWith("pom.xml"))
      {
        pomEntry = entry1;
        need--;
      }
      if (need <= 0) {
        break;
      }
    }
    POM pom = new POM();
    
    InputStream input = jarFile.getInputStream(entry);
    Properties p = new Properties();
    p.load(input);
    pom.setJarName(jarName);
    Enumeration enu2 = p.propertyNames();
    int needPro = 3;
    while (enu2.hasMoreElements())
    {
      String key = (String)enu2.nextElement();
      if ("version".equalsIgnoreCase(key))
      {
        pom.setVersion(p.getProperty(key));
        needPro--;
      }
      else if ("groupId".equalsIgnoreCase(key))
      {
        pom.setGroupId(p.getProperty(key));
        needPro--;
      }
      else if ("artifactId".equalsIgnoreCase(key))
      {
        pom.setArtifactId(p.getProperty(key));
        needPro--;
      }
      if (needPro <= 0) {
        break;
      }
    }
    String name = new File(jarName).getName();
    int index = name.lastIndexOf(".");
    pom.setPackaging(name.substring(index + 1));
    
    InputStream input1 = jarFile.getInputStream(pomEntry);
    File jar = new File(jarName);
    String parentStr = jar.getParent() + File.separator + "pom" + File.separator;
    File parent = new File(parentStr);
    if (!parent.exists()) {
      parent.mkdirs();
    }
    String pomPath = parentStr + name.substring(0, index) + ".pom";
    copy(input1, new FileOutputStream(pomPath));
    pom.setPomFile(pomPath);
    
    String src = jar.getParent() + File.separator + name.substring(0, index) + "-sources" + ".jar";
    File srcFile = new File(src);
    if (srcFile.exists()) {
      pom.setSrcFile(srcFile.getAbsolutePath());
    }
    String doc = jar.getParent() + File.separator + name.substring(0, index) + "-javadoc" + ".jar";
    File docFile = new File(doc);
    if (docFile.exists()) {
      pom.setJavadoc(docFile.getAbsolutePath());
    }
    jarFile.close();
    return pom;
  }
  
  public static int copy(InputStream in, OutputStream out)
    throws IOException
  {
    int byteCount = 0;
    byte[] buffer = new byte['?'];
    int bytesRead = -1;
    while ((bytesRead = in.read(buffer)) != -1)
    {
      out.write(buffer, 0, bytesRead);
      byteCount += bytesRead;
    }
    out.flush();
    return byteCount;
  }
  
  protected String getCommonOrder(POM pom)
  {
    StringBuilder sb = new StringBuilder();
    sb.append(" -Dfile=" + new File(pom.getJarName()).getAbsolutePath());
    sb.append(" -DgroupId=" + pom.getGroupId());
    sb.append(" -DartifactId=" + pom.getArtifactId());
    sb.append(" -Dversion=" + pom.getVersion());
    sb.append(" -Dpackaging=" + pom.getPackaging());
    if ((pom.getPomFile() != null) && (!"".equals(pom.getPomFile()))) {
      sb.append(" -DpomFile=" + pom.getPomFile());
    }
    if ((pom.getSrcFile() != null) && (!"".equals(pom.getSrcFile()))) {
      sb.append(" -Dsources=" + pom.getSrcFile());
    }
    if ((pom.getJavadoc() != null) && (!"".equals(pom.getJavadoc()))) {
      sb.append(" -Djavadoc=" + pom.getJavadoc());
    }
    return sb.toString();
  }
  
  public class POM
  {
    private String jarName;
    private String version;
    private String groupId;
    private String artifactId;
    private String packaging = "jar";
    private String pomFile;
    private String srcFile;
    private String javadoc;
    
    public POM() {}
    
    public String getVersion()
    {
      return this.version;
    }
    
    public void setVersion(String version)
    {
      this.version = version;
    }
    
    public String getGroupId()
    {
      return this.groupId;
    }
    
    public void setGroupId(String groupId)
    {
      this.groupId = groupId;
    }
    
    public String getArtifactId()
    {
      return this.artifactId;
    }
    
    public void setArtifactId(String artifactId)
    {
      this.artifactId = artifactId;
    }
    
    public String toString()
    {
      return 
      
        "POM [jarName=" + this.jarName + ", version=" + this.version + ", groupId=" + this.groupId + ", artifactId=" + this.artifactId + ", packaging=" + this.packaging + ", pomFile=" + this.pomFile + ", srcFile=" + this.srcFile + ", javadoc=" + this.javadoc + "]";
    }
    
    public String getJarName()
    {
      return this.jarName;
    }
    
    public void setJarName(String jarName)
    {
      this.jarName = jarName;
    }
    
    public String getPackaging()
    {
      return this.packaging;
    }
    
    public void setPackaging(String packaging)
    {
      this.packaging = packaging;
    }
    
    public String getPomFile()
    {
      return this.pomFile;
    }
    
    public void setPomFile(String pomFile)
    {
      this.pomFile = pomFile;
    }
    
    public String getSrcFile()
    {
      return this.srcFile;
    }
    
    public void setSrcFile(String srcFile)
    {
      this.srcFile = srcFile;
    }
    
    public String getJavadoc()
    {
      return this.javadoc;
    }
    
    public void setJavadoc(String javadoc)
    {
      this.javadoc = javadoc;
    }
  }
}
